package com.q.tqb.activity.mine.setting;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonElement;
import com.q.tqb.R;
import com.q.tqb.activity.mine.wallet.WalletAddCardActivity;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseActivity;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.SPUtil;
import com.q.tqb.util.StringUtil;
import com.q.tqb.util.http.IResponse;
import com.q.tqb.util.http.OkHttpUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingPayPassActivity extends BaseActivity {


    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.et_pass)
    EditText mEtPass;
    @BindView(R.id.et_pass1)
    EditText mEtPass1;
    @BindView(R.id.bt_send)
    Button mBtSend;
    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    @BindView(R.id.ll_code)
    LinearLayout mLlCode;

    private int uid;

    private MyCountDownTimer myCountDownTimer;

    @Override
    public int getLayoutId() {
        return R.layout.activity_setting_pay_pass;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        setTitle("支付密码设置");
        uid = (int) SPUtil.get(this, "uid", 0);
        String nickName = SPUtil.getString(this, "nickname");
        mTvName.setText(nickName);

        myCountDownTimer = new MyCountDownTimer(1000 * 120, 1000);
        String password = SPUtil.getString(this, "payPassword");
        if (!TextUtils.isEmpty(password)) {
            mEtPass.setText("******");
            mEtPass1.setText("******");
            mEtPass.setEnabled(false);
            mEtPass1.setEnabled(false);
            mBtSend.setVisibility(View.GONE);
            mLlCode.setVisibility(View.GONE);
            setTvRight("修改");
        }

    }


    @OnClick({R.id.iv_back, R.id.bt_send, R.id.tv_right, R.id.tv_get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.bt_send:
                setPayPass();
                break;
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.tv_right:
                if (mTvRight.getText().equals("修改")) {
                    setTvRight("取消");
                    mEtPass.setText("");
                    mEtPass1.setText("");
                    mEtPass.setEnabled(true);
                    mEtPass1.setEnabled(true);
                    mBtSend.setVisibility(View.VISIBLE);
                    mLlCode.setVisibility(View.VISIBLE);
                } else {
                    setTvRight("修改");
                    mEtPass.setText("******");
                    mEtPass1.setText("******");
                    mEtPass.setEnabled(false);
                    mEtPass1.setEnabled(false);
                    mBtSend.setVisibility(View.GONE);
                    mLlCode.setVisibility(View.GONE);
                }
                break;
            default:
                finish();
        }
    }

    private void getCode() {
        String phone = SPUtil.getString(SettingPayPassActivity.this, "mobile");
        if (TextUtils.isEmpty(phone)) {
            Intent intent = new Intent(SettingPayPassActivity.this, SettingBindTelActivity.class);
            startActivity(intent);
        } else {
            Map<String, String> map = new HashMap<>();
            map.put("mobile", phone + "");
            map.put("type", 4 + "");
            mProgressDialog.show();
            OkHttpUtil.getInstance().post(this, AppConst.URL_GET_CODE, map, new IResponse() {
                @Override
                public void onSuccess(JsonElement element) {
                    LogUtil.i(element.toString());
                    mProgressDialog.dismiss();
                    showToast("发送成功");
                    myCountDownTimer.start();
                }

                @Override
                public void onFailure(int statusCode, String errorMsg) {
                    LogUtil.i(errorMsg + "statusCode" + statusCode);
                    showToast(errorMsg);
                    mProgressDialog.dismiss();
                }
            });
        }
    }

    private void setPayPass() {

        String password = mEtPass.getText().toString();
        String password1 = mEtPass1.getText().toString();
        String code = mEtCode.getText().toString();

        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            showToast("请输入验证码");
            return;
        }
        if (password.length() != 6) {
            showToast("密码只能设置6位");
            return;
        }
        if (!password.equals(password1)) {
            showToast("两次密码输入不一致");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("uid", uid + "");
        map.put("pay_password", password);
        map.put("code", code);
        map.put("token", StringUtil.getToken(uid));
        mProgressDialog.show();
        OkHttpUtil.getInstance().post(this, AppConst.URL_PAY_PASSWORD, map, new IResponse() {
            @Override
            public void onSuccess(JsonElement element) {
                LogUtil.i(element.toString());
                mProgressDialog.dismiss();
                SPUtil.put(SettingPayPassActivity.this, "payPassword", "payPassword");
                showToast("支付密码设置成功");
                setResult(RESULT_OK);
                finish();
            }

            @Override
            public void onFailure(int statusCode, String errorMsg) {
                LogUtil.i(errorMsg + "statusCode" + statusCode);
                showToast(errorMsg);
                mProgressDialog.dismiss();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCountDownTimer != null) {
            myCountDownTimer.cancel();
        }
    }


    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        //计时过程
        @Override
        public void onTick(long l) {
            //防止计时过程中重复点击
            mTvGetCode.setClickable(false);
            mTvGetCode.setText(l / 1000 + "s");
        }

        //计时完毕的方法
        @Override
        public void onFinish() {
            //重新给Button设置文字
            mTvGetCode.setText("获取验证码");
            //设置可点击
            mTvGetCode.setClickable(true);
        }
    }

}
